package com.seadragon.app.ksr.dao.jpa;

import java.util.List;

import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.seadragon.app.ksr.dao.CountyDao;
import com.seadragon.app.ksr.model.tiger.County;

@SuppressWarnings("unchecked")
@Repository("countyDao")
public class CountyDaoJpaImpl extends AbstractJpaDao implements CountyDao {
	private static Logger logger = LoggerFactory.getLogger(CountyDaoJpaImpl.class);

	@Override
	public List<County> findAdjacentCounties(String countyName, String statefp) {
		logger.info("Entering ... ");
		Query q = entityManager.createNamedQuery("findAdjacentCounties").setParameter("countyName", countyName).setParameter("statefp", statefp);
		q.setHint("org.hibernate.readOnly",true);
		logger.info("Leaving ...");
		return (List<County>)q.getResultList();
	}

	@Override
	public List<County> findAll(int offset, int pageSize) {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<County> cq = cb.createQuery(County.class);
		Root<County> root = cq.from(County.class);
		cq.select(root);
		TypedQuery<County> tq = entityManager.createQuery(cq);
		tq.setFirstResult(offset);
		tq.setMaxResults(pageSize);
		return tq.getResultList();
	}
}
